local BagLogic = require("bag.BagLogic")
local Msg = require("core.Msg")
local Broadcast = require("broadcast.Broadcast")
local Lang = require("common.Lang")
local EquipLogic = require("bag.EquipLogic")
local XLConfig = require("excel.Xilian")
local EquipConfig = require("excel.Equip")
local EquipScore = EquipConfig.equipScore
local Grid = require("bag.Grid")
local ObjHuman = require("core.ObjHuman")
local ShopLogic = require("shop.ShopLogic")
local RefineHandle = require("refine.HandlerL")
local common = XLConfig.common
local ShopHelp = require("shop.ShopHelp")

function notRepeat(attrs, key)
  for i=1, attrs[0] do
    if attrs[i].key == key then
      return false
    end
  end
  return true
end

function getRandStar(xiLianTimes, needTimes2, xlLv)
  local total = 10000
  local rand = math.random(1, total)
  total = 0
  for i=1, #common[xlLv].weight do
    total = total + common[xlLv].weight[i]
    if rand < total then
      if xiLianTimes >= needTimes2 and i == 10 then
        return i
      elseif i == 10 then
        return i - 1
      else
        return i
      end
    end
  end
  return 1
end

function getRandAttr(attrs, id, xiLianTimes, xlLv)
  local thisXlConf = XLConfig[EquipConfig.equip[id].xilianId]
  attrs[0] = 0
  while attrs[0] < 4 do
    local totalWeight = 0
    for i=1, #thisXlConf do
      if xiLianTimes >= thisXlConf[i].needTimes1 then
        totalWeight = totalWeight + thisXlConf[i].weight
      end
    end
    local rand = math.random(1, totalWeight)
    totalWeight = 0
    for i=1, #thisXlConf do
      if xiLianTimes >= thisXlConf[i].needTimes1 then
        totalWeight = totalWeight + thisXlConf[i].weight
        if rand < totalWeight and notRepeat(attrs, thisXlConf[i].attrId) and attrs[0] < 4 then          
          local star = getRandStar(xiLianTimes, thisXlConf[i].needTimes2, xlLv)
          attrs[0] = attrs[0] + 1
          attrs[attrs[0] ].key = thisXlConf[i].attrId
          attrs[attrs[0] ].value = thisXlConf[i].values[star]
          attrs[attrs[0] ].star = star
          break      
        end        
      end
    end
   end
end
function getPower(attrs)
  local zhandouli = 0
  for i=0, 3 do
    zhandouli = zhandouli + EquipScore[attrs[i].key].score * attrs[i].value 
  end
  return math.floor(zhandouli)
end

function xilian(human, isAutoBuy, index)

  local grid = human.db.equip[index]
  local equip = grid.data.equip
  local data = human.db.xlLv[index]
 
  if ObjHuman.getYinliang(human) < common[data.lv].needYinliang then
    return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.REFINE_YINLIANG_NOT_ENOUGH)
  end
  if BagLogic.getItemCount(human, common[data.lv].needItem[1]) < common[data.lv].needItem[2] then
    if isAutoBuy ~= 1 then
      return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.REFINE_XIANLIANSTONE_NOT_ENOUGH)
    else
      if ObjHuman.getYuanbao(human) < 20 then
        ShopHelp.onNoRMB(human)
        return
      end
      ObjHuman.decYuanbao(human, 20, "xilian")
    end
  else
    BagLogic.delItemsById(human, common[data.lv].needItem[1], common[data.lv].needItem[2], "xilian")
  end  
  ObjHuman.decYinliang(human, common[data.lv].needYinliang, "xilian")
  
  local sendMsg = Msg.gc.GC_EQUIP_XILIAN_ATTR
  getRandAttr(sendMsg.attrs, grid.id, equip.xiLianTimes, data.lv)
  for i=0, 3 do    
    human.db.xilianAttrTemp[i].key = sendMsg.attrs[i+1].key
    human.db.xilianAttrTemp[i].value = sendMsg.attrs[i+1].value
    human.db.xilianAttrTemp[i].star = sendMsg.attrs[i+1].star
  end
  sendMsg.zhandouli = getPower(human.db.xilianAttrTemp)
  equip.xiLianTimes = equip.xiLianTimes + 1
  if not (data.lv == #common and data.exp > common[data.lv].needExp) then
    data.exp = data.exp + common[data.lv].addExp
  end
  if data.exp >= common[data.lv].needExp and data.lv < #common then
    data.exp = data.exp - common[data.lv].needExp
    data.lv = data.lv + 1    
  end
  sendMsg.curLv = data.lv
  sendMsg.curExp = data.exp
  sendMsg.needExp = common[data.lv].needExp
  sendMsg.starRand[0] = 2
  sendMsg.starRand[1] = common[data.lv].showStar[1]
  sendMsg.starRand[2] = common[data.lv].showStar[2]
  Msg.send(sendMsg, human.fd)  
  for i=1, 4 do
     sendMsg.attrs[i].key = 0  
  end
  RefineHandle.CG_OPEN_REFINE_WINDOW(human)
end

function save(human,  index)
  local equip = human.db.equip[index].data.equip

  if 0 == human.db.xilianAttrTemp[1].key then
    return
  end

  for i=0, 3 do
    equip.xiLianAttr[i] = human.db.xilianAttrTemp[i].key
    equip.xiLianAttrVal[i] = human.db.xilianAttrTemp[i].value
    equip.xiLianStar[i] = human.db.xilianAttrTemp[i].star
  end

  equip.xiLianPower = getPower(human.db.xilianAttrTemp)
  EquipLogic.GC_EQUIP_CHANGE(human,index)
  ObjHuman.doCalc(human)
  human.db.xilianAttrTemp = {}
end

function chooseEquip(human, index)
  local sendMsg = Msg.gc.GC_CHOOSE_EQUIP
  local grid = human.db.equip[index]
  local thisXlConf = XLConfig[EquipConfig.equip[grid.id].xilianId]
  local data = human.db.xlLv[index]
  
  sendMsg.isMax = 0
  sendMsg.type = 4
  sendMsg.showRand = 0
  sendMsg.needYinliang = common[data.lv].needYinliang
  sendMsg.needItem[0] = 1
  sendMsg.nextEquip[0] = 0
  sendMsg.stageNextLv = 0
  local cache = {}
  Grid.create(cache, common[data.lv].needItem[1], common[data.lv].needItem[2])
  Grid.makeItem(cache, sendMsg.needItem[1], 0, human)
  
  sendMsg.jipin[0] = 5
  for i=1, 5 do
    for j=1, #thisXlConf do
      if common[data.lv].jipin[i] == thisXlConf[j].attrId then
        sendMsg.jipin[i].key = common[data.lv].jipin[i]
        sendMsg.jipin[i].value = thisXlConf[j].values[10]
      end
    end    
  end
  Msg.send(sendMsg, human.fd)
end

function upXilianAttr(human, index)
  local grid = human.db.equip[index]
  local equip = grid.data.equip
  local thisXlConf = XLConfig[EquipConfig.equip[grid.id].xilianId]
  if equip.xiLianStar[0] == 0 then
    return
  end
  local power = 0
  for i=0, 3 do
    for j=1, #thisXlConf do
      if thisXlConf[j].attrId == equip.xiLianAttr[i] then      
        equip.xiLianAttrVal[i] = thisXlConf[j].values[equip.xiLianStar[i]]
        power = power + EquipScore[equip.xiLianAttr[i]].score * equip.xiLianAttrVal[i]
      end
    end
  end
  equip.xiLianPower = power
end

function checkShowTips(human, info)
  if human.db.lv < 45 then
    return 0
  end
  local cache = {}
  for i=0,human.db.equipLen - 1 do
    local grid = human.db.equip[i]
    local data = human.db.xlLv[i]
    if grid ~= nil and grid.id ~= 0 then
      if BagLogic.getItemCount(human, common[data.lv].needItem[1]) >= common[data.lv].needItem[2] and ObjHuman.getYinliang(human) > common[data.lv].needYinliang then
        if info then
          info.equip[0] = 1
          Grid.create(cache, grid.id, 1)
          Grid.makeItem(cache, info.equip[1], 0, human)
        end
        return 1
      end
    end
  end
  return 0
end

